package database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import partnermanagement.Eltern;
import partnermanagement.IEltern;
import partnermanagement.IKind;


public class ElternDAO implements IElternDAO{
	private IKindDAO kindDAO;
	private  IDBConnector idbconnector;
	
	public ElternDAO() {
		this.idbconnector = DBConnectorMySQL.getInstance();
		this.kindDAO = new KindDAO();
	}
	
	/* (non-Javadoc)
	 * @see database.IEltern#addEltern(partnermanagement.IEltern)
	 */
	/* (non-Javadoc)
	 * @see database.IElternDAO#addEltern(partnermanagement.IEltern)
	 */

	@Override
	public void addEltern(IEltern eltern){
		// TODO
	}
	
	/* (non-Javadoc)
	 * @see database.IEltern#updateEltern(partnermanagement.IEltern)
	 */
	/* (non-Javadoc)
	 * @see database.IElternDAO#updateEltern(partnermanagement.IEltern)
	 */

	@Override
	public void updateEltern(IEltern eltern){
		// TODO
	}
	
	/* (non-Javadoc)
	 * @see database.IEltern#deleteEltern(partnermanagement.IEltern)
	 */
	/* (non-Javadoc)
	 * @see database.IElternDAO#deleteEltern(partnermanagement.IEltern)
	 */

	@Override
	public void deleteEltern(IEltern eltern){
		// TODO
	}
	
	
	/* (non-Javadoc)
	 * @see database.IEltern#findAll()
	 */
	/* (non-Javadoc)
	 * @see database.IElternDAO#findAll()
	 */

	@Override
	public List<IEltern> findAll(){
		ResultSet resultSet = null;
		try {
			
			String Statement = ("select * from Eltern;");
			resultSet = idbconnector.query(Statement);
		 } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return buildEltern(resultSet);
		
	}
	
	/* (non-Javadoc)
	 * @see database.IEltern#getElternByKind(partnermanagement.IKind)
	 */
	/* (non-Javadoc)
	 * @see database.IElternDAO#getElternByKind(partnermanagement.IKind)
	 */

	@Override
	public IEltern getElternByKind(IKind kind){
		IEltern result =  null;
		ResultSet resultSet =  null;
		// TODO Username != UserID, Umwandlung quatsch
		String Username = String.valueOf(kind.getId());
		try {
			
			String Statement = ("select * from Eltern where Eltern.Username in (select Kind.Eltern from Kind where Kind.Eltern = " + Username + ";");
			
			resultSet = idbconnector.query(Statement);
				
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		List<IEltern> eltern = buildEltern(resultSet);
		if(eltern.size() > 0 ){
			return eltern.get(0);
		}
		return result;
	}


	private List<IEltern> buildEltern(ResultSet resultSet) {
		List<IEltern> result = new ArrayList<IEltern>();
		try {
			while (resultSet.next()) {
				String Username = resultSet.getString("Username");
	            String Vorname = resultSet.getString("Vorname");
	            String Nachname = resultSet.getString("Nachname");
	            IEltern eltern = new Eltern(Username, Vorname, Nachname);
	                                 
                    
	            result.add(eltern);
	        }	
			 } catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			 return result;
	}
}
